﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using MySql.Data.Types;

namespace WindowsFormsApplication5
{
    public partial class Form1 : Form
    {
        MySqlConnection grabConnection = new MySqlConnection("Server=instance8003.db.xeround.com;Port=6006;Database=grabcollab;Uid=cookicom0;Pwd=pizzahut91;");
        int userID;
        int projectAmount;

        //hier komen title en description voor de projecten in
        string title;
        string description;
        string title2;
        string description2;
        string title3;
        string description3;
        string title4;
        string description4;
        string title5;
        string description5;
        //hier komen de project ID's in
        int[] projects = new int[50];
        MySqlDataReader reader = null;

        public Form1()
        {

        }


        public Form1(string name, string pass)
        {
            InitializeComponent();
            progressBar1.Minimum = 0;
            progressBar1.Maximum = 100;
            progressBar1.Step = 18;
            string username = name;
            string password = pass;
       
            try
            {
               
                grabConnection.Open();
                //userID ophalen
                MySqlCommand getID = new MySqlCommand("SELECT UserID FROM confirmed_users WHERE Email='" + username + "'", grabConnection);

                reader = getID.ExecuteReader();

                //userID ophalen
                while (reader.Read())
                {
                    userID = Convert.ToInt32(reader["userID"].ToString());
                }

                grabConnection.Close();

                grabConnection.Open();

                MySqlCommand getProjectAmount = new MySqlCommand("SELECT COUNT(UserID) AS AMOUNT FROM project_members WHERE UserID='" + userID + "'", grabConnection);
                reader = getProjectAmount.ExecuteReader();

                //aantal projecten ophalen
                while (reader.Read())
                {
                    projectAmount = Convert.ToInt32(reader["AMOUNT"].ToString());
                }

                grabConnection.Close();

                grabConnection.Open();

                MySqlCommand getProjectID = new MySqlCommand("SELECT ProjectID FROM project_members WHERE UserID='" + userID + "' ORDER BY ProjectID", grabConnection);
                reader = getProjectID.ExecuteReader();

                //projectIDID ophalen
                int counter = 0;
                while (reader.Read())
                {
                    projects[counter] = Convert.ToInt32(reader["ProjectID"].ToString());
                    counter++;
                }

                grabConnection.Close();
            }
            catch (MySqlException)
            {
                MessageBox.Show("Sql error detected");
                return;
            }
            ///////////////////////////////////////////////////////////////////////////////////////////
            progressBar1.PerformStep();
            ///////////////////////////////////////////////////////////////////////////////////////////
            if (projectAmount >= 1)
            {
                try
                {
                    //projecttitel en beschrijving ophalen
                    grabConnection.Open();

                    MySqlCommand getProjectInfo = new MySqlCommand("SELECT Projectnaam, Projectbeschrijving FROM projects WHERE ProjectID ='" + projects[0] + "'", grabConnection);
                    reader = getProjectInfo.ExecuteReader();

                    while (reader.Read())
                    {
                        title = reader["Projectnaam"].ToString();
                        description = reader["Projectbeschrijving"].ToString();
                    }
                    reader.Dispose();
                }
                catch (MySqlException)
                {
                    MessageBox.Show("Sql error detected! @ project 1");
              
                }
            }
            //////////////////////////////////////////////////////////////////////////////////////////
            progressBar1.PerformStep();
            ///////////////////////////////////////////////////////////////////////////////////////////
            if (projectAmount >= 2)
            {
                try
                {
                    //projecttitel en beschrijving ophalen


                    MySqlCommand getProjectInfo = new MySqlCommand("SELECT Projectnaam, Projectbeschrijving FROM projects WHERE ProjectID ='" + projects[1] + "'", grabConnection);
                    MySqlDataReader reader2 = getProjectInfo.ExecuteReader();

                    while (reader2.Read())
                    {
                        title2 = reader2["Projectnaam"].ToString();
                        description2 = reader2["Projectbeschrijving"].ToString();
                    }
                    reader2.Dispose();
                }
                catch (MySqlException e)
                {
                    MessageBox.Show("Sql error detected! @ project 2");
                    Console.Write(e);
                }
            }
            ///////////////////////////////////////////////////////////////////////////////////////////
            progressBar1.PerformStep();
            ///////////////////////////////////////////////////////////////////////////////////////////
            if (projectAmount >= 3)
            {
                try
                {
                    //projecttitel en beschrijving ophalen


                    MySqlCommand getProjectInfo = new MySqlCommand("SELECT Projectnaam, Projectbeschrijving FROM projects WHERE ProjectID ='" + projects[2] + "'", grabConnection);
                    MySqlDataReader reader3 = getProjectInfo.ExecuteReader();

                    while (reader3.Read())
                    {
                        title3 = reader3["Projectnaam"].ToString();
                        description3 = reader3["Projectbeschrijving"].ToString();
                    }
                    reader3.Dispose();
                }
                catch (MySqlException)
                {
                    MessageBox.Show("Sql error detected! @ project 3");
                }
            }
            ///////////////////////////////////////////////////////////////////////////////////////////
            progressBar1.PerformStep();
            ///////////////////////////////////////////////////////////////////////////////////////////
            if (projectAmount >= 4)
            {
                try
                {
                    //projecttitel en beschrijving ophalen


                    MySqlCommand getProjectInfo = new MySqlCommand("SELECT Projectnaam, Projectbeschrijving FROM projects WHERE ProjectID ='" + projects[3] + "'", grabConnection);
                    MySqlDataReader reader4 = getProjectInfo.ExecuteReader();

                    while (reader4.Read())
                    {
                        title4 = reader4["Projectnaam"].ToString();
                        description4 = reader4["Projectbeschrijving"].ToString();
                    }
                    reader4.Dispose();
                }
                catch (MySqlException)
                {
                    MessageBox.Show("Sql error detected! @ project 4");
                }
            }
            ///////////////////////////////////////////////////////////////////////////////////////////

            progressBar1.PerformStep();
            ///////////////////////////////////////////////////////////////////////////////////////////
            if (projectAmount == 5)
            {

                try
                {
                    //projecttitel en beschrijving ophalen


                    MySqlCommand getProjectInfo = new MySqlCommand("SELECT Projectnaam, Projectbeschrijving FROM projects WHERE ProjectID ='" + projects[4] + "'", grabConnection);
                    MySqlDataReader reader5 = getProjectInfo.ExecuteReader();

                    while (reader5.Read())
                    {
                        title5 = reader5["Projectnaam"].ToString();
                        description5 = reader5["Projectbeschrijving"].ToString();
                    }
                    reader5.Dispose();
                    grabConnection.Close();
                }
                catch (MySqlException)
                {
                    MessageBox.Show("Sql error detected! @ project 5");
                }
               


            }
            progressBar1.PerformStep();
            Projectkeuzemenu.Projectkeuzemenu loadingDone = new Projectkeuzemenu.Projectkeuzemenu(name, pass, projectAmount, title, description, title2, description2, title3, description3, title4, description4, title5, description5);
            loadingDone.Show();
        }
        private void progressBar1_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
          
       
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

    }
}
